<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\File;
use Carbon\Carbon;
use Illuminate\Http\Request;

class MaterialService
{

    /**
     * 素材管理获取图片
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MaterialPicFileService( $request )
    {
        $where  = function ( $query ) use ( $request ) {
            if ( $request->filled('ext') and $request->input('ext') != '' ) $query->where('ext', $request->input('ext'));
            if ( $request->filled('uid') and $request->input('uid') != '' ) $query->where('uid', $request->input('uid'));
            $query->where('status', 1);
            $query->where('user', 'blog');
            $query->where('is_show', $request->input('is_show', 2));
        };
        $select = [ 'updated_at', 'id', 'url', 'name', 'file_url', 'ext', 'category', 'uid', 'user' ];
        if ( $request->input('status') ) {
            $file                = File::GetModel($where, $select);
            $material[ 'start' ] = 0;
            foreach ( $file as $key => $value ) {
                $material[ 'id' ]    = $request->input('category');
                $material[ 'title' ] = $request->input('title');
                if ( $value->id == $request->input('id') ) $material[ 'start' ] = $key;
                $material[ 'data' ][ $key ][ 'alt' ]   = $value->name;
                $material[ 'data' ][ $key ][ 'pid' ]   = $value->id;
                $material[ 'data' ][ $key ][ 'src' ]   = $value->url;
                $material[ 'data' ][ $key ][ 'thumb' ] = $value->url;
            }
        }
        else $material = File::PaginateModel($where, [], $select);
        return data($material);
    }


    /**
     * 素材管理的删除
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MaterialDestroyService( $request )
    {
        $select           = [ 'id', 'status', 'deleted_at' ];
        $file             = File::FirstModel($request->input('id'), $select);
        $file->status     = 2;
        $file->deleted_at = Carbon::now()->toDateTimeString();
        $file->save();
        return data();
    }

}
